package com.denlaku.longan.core;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import jakarta.servlet.http.HttpSession;

import java.time.Duration;

/**
 * @author tianx
 */
public class HttpSessionManager {
    private static final Cache<Long, HttpSession> SESSION_CACHE = CacheBuilder
            .newBuilder()
            .expireAfterWrite(Duration.ofMinutes(30))
            .build();

    public static void putSession(Long userId, HttpSession httpSession) {
        SESSION_CACHE.put(userId, httpSession);
    }

    public static void removeSession(Long userId) {
        if (userId != null) {
            SESSION_CACHE.invalidate(userId);
        }
    }

    public static HttpSession getSession(Long userId) {
        if (userId == null) {
            return null;
        }
        return SESSION_CACHE.getIfPresent(userId);
    }

}
